package com.genius.reservation.car.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genius.reservation.car.entity.CarInformation;
import com.genius.reservation.car.entity.ReservationStartCarInformation;
import com.genius.reservation.car.service.ICarInformationService;
import com.genius.reservation.common.ResponseRule;
import com.genius.reservation.common.constant.Constant;
import com.genius.reservation.driver.entity.DriverInformation;
import com.genius.reservation.driver.service.IDriverInformationService;
import com.genius.reservation.mapper.CarInformationMapper;
import com.genius.reservation.mapper.DriverInformationMapper;
import com.genius.reservation.mapper.ReservationInformationMapper;
import com.genius.reservation.mapper.ReservationStartCarInformationMapper;
import com.genius.reservation.reservation.service.IReservationInformationService;
import com.genius.reservation.tools.StringTrim;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * <p>
 * 车辆信息表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2022-05-26
 */
@Service
@Transactional
public class CarInformationServiceImpl extends ServiceImpl<CarInformationMapper, CarInformation> implements ICarInformationService {


    @Autowired

    IDriverInformationService driverService;

    @Autowired
    @Lazy
    DriverInformationMapper driverMapper;
    @Autowired
    CarInformationMapper carMapper;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseRule bindCar(Map<String, String> body) {
        CarInformation carCode = carMapper.selectOneById(body.get("carCode"));
        if (carCode != null)
            return ResponseRule.no("该车牌号已与其他司机绑定,无法继续绑定");
        CarInformation carInformation = new CarInformation();
        carInformation.setCarCode(StringTrim.Ex(body.get("carCode").toUpperCase()));
        carInformation.setCarNumber(Constant.CAR_PREFIX + IdUtil.randomUUID());
        carInformation.setCarPic(body.get("carPic"));
        carInformation.setDriverNumber(body.get("driverNumber"));
        carInformation.setCarHeight(body.get("carHeight"));
        carInformation.setCarType(body.get("carType"));
        carInformation.setCarLength(body.get("carLength"));
        carInformation.setCarWidth(body.get("carWidth"));
        CarInformation car = this.addAndUp(carInformation);
        DriverInformation driverNumber = driverMapper.selectDriverNoCar(body.get("driverNumber"));
        System.out.println(driverNumber);
        driverNumber.setCarNumber(car.getCarNumber());
        driverService.addAndUp(driverNumber);
        return ResponseRule.ok(car, "绑定成功");
    }




}
